<?use pft\Member\MemberAccount;//要放在文件的第一行位置
include_once('../common/autoload.php');
if(!$_SESSION['memberID']){exit("<script>window.location='dlogin.html'</script>");}
$id = $_REQUEST["id"];
$dtype = $_REQUEST["dtype"];
$parent_id = ($dtype == 0) ? $_SESSION['sid']:$id;
$son_id = ($dtype == 0) ? $id:$_SESSION['sid'];
$mem = new MemberAccount($GLOBALS['le']);//实例化时需要注入mysql连接标识
//echo json_encode(array('id'=>$id,'dtype'=>$dtype,'parent_id'=>$parent_id,'sessionmemberID'=>$_SESSION['memberID'],'son_id'=>$son_id));
// echo MemberAccount::RELATION_REMOVE,';',$parent_id,';',$son_id;
// exit;
// 只解除，没删除，是一个问题。 1 4 138 
// 将关系表status 置为 1
// echo json_encode($mem->RelationHanler(MemberAccount::RELATION_REMOVE, $parent_id, $son_id));
if($parent_id==$son_id) exit('{"status":"fail","msg":"不能添加自己"}');

// 会员卡账号不允许被添加为分销商
// $sql = "select id from pft_member_card where memberID=$son_id limit 1";
// $GLOBALS['le']->query($sql);
// if($GLOBALS['le']->fetch_assoc()) exit('{"status":"fail","msg":"供应商专属会员，添加失败！您可换个手机号注册"}');

// 类型判断
// $sql = "select dtype from pft_member where id=$son_id limit 1";
// $GLOBALS['le']->query($sql);
// $GLOBALS['le']->fetch_assoc();
// $ship_type = ($GLOBALS['le']->f('dtype')==6)? 1:0;
$ship_type = 0;
$result = $mem->RelationHanler(MemberAccount::RELATION_REMOVE, $parent_id, $son_id, $ship_type);

if($result['status']=='ok'){

	// 1.联盟盟主删除分销商、供应商则关系都删除，并把会员退出联盟
	if($_SESSION['alliance'] && ($son_id==$_SESSION['sid'] || $parent_id==$_SESSION['sid'])){
		
		$mem->RelationHanler(MemberAccount::RELATION_REMOVE, $son_id, $parent_id);
		$sid = $_SESSION['sid'];
		$mid = ($_SESSION['sid']==$son_id) ? $parent_id:$son_id;
		$sql = "update `pft_union_member_info_SE` set dstatus=1 where fid=$sid and memberID=$mid limit 1";
		$GLOBALS['le']->query($sql);
		
	}else{
		$sid = ($_SESSION['sid']==$son_id) ? $parent_id:$son_id;
		$mid = $_SESSION['sid'];
		// 被删除的会员是否为联盟账号
		$sql = "select id from pft_distributor_union_SE where fid=$parent_id limit 1";
		$GLOBALS['le']->query($sql);
		if($GLOBALS['le']->fetch_assoc()){
			// 2.供应商删除分销商或供应商
			$mem->RelationHanler(MemberAccount::RELATION_REMOVE, $son_id, $parent_id);
			$sql = "update `pft_union_member_info_SE` set dstatus=1 where fid=$sid and memberID=$mid limit 1";
			$GLOBALS['le']->query($sql);
		}	
	}
	// 清除代理商
	$sql = "update pft_distributor_agency set dstatus=1 where fid=$sid and inviterID=$mid";
	$GLOBALS['le']->query($sql);
}

exit(json_encode($result));
?>